package com.unis.db;


/*
�ҵ���ݿ����ӳ�

***********ģ��˵��**************

getInstance()����POOLΨһʵ��,��һ�ε���ʱ��ִ�й��캯��
���캯��Pool()������װ��loadDrivers()����;���ӳش���createPool()����
loadDrivers()װ����
createPool()�����ӳ�
getConnection()����һ������ʵ��
getConnection(long time)���ʱ������
freeConnection(Connection con)��con����ʵ��ص����ӳ�
getnum()���ؿ���������
getnumActive()���ص�ǰʹ�õ�������

*/

import java.io.*;
import java.sql.*;
import java.util.*;
import java.util.Date;


public class Pool {

static private Pool instance = null; //����Ψһʵ��

private int maxConnect = 100;//���������
private int normalConnect = 10;//����������
private String password = "";//����
private String url = "";//����URL
private String user = "";//�û���
private String driverName = "";//����

Driver driver = null;//�����
DBConnectionPool pool = null;//���ӳ�ʵ�����
//public static void main(String[] args) {
//	Pool.getInstance();
//	
//}
//����Ψһʵ��
static synchronized public Pool getInstance(String dbName,String userName,String passWord,String port,String urll,String dbtype) {
    if (instance == null) {
      instance = new Pool(dbName,userName,passWord,port,urll,dbtype);
    }

    return instance;
}

//�����캯��˽��,������������
private Pool(String dbName,String userName,String passWord,String port,String urll,String dbtype) {
	user=userName;
	password=passWord;
	if(dbtype.equalsIgnoreCase("sqlserver")){//sqlserver
		url="jdbc:microsoft:sqlserver://" + urll  + ":" + port + ";DatabaseName=" + dbName + "";
		driverName="com.microsoft.jdbc.sqlserver.SQLServerDriver";
	    loadDrivers(driverName);
	    createPool();
	}else if(dbtype.equalsIgnoreCase("oracle")){//oracle
		url="jdbc:oracle:thin:@" + urll + ":" + port + ":" + dbName;
		driverName = "oracle.jdbc.driver.OracleDriver";
	    loadDrivers(driverName);
	    createPool();
	}else if(dbtype.equalsIgnoreCase("mysql")){//mysql
		url="jdbc:mysql://"+urll+":"+port+"/"+dbName;
		driverName = "com.mysql.jdbc.Driver";
	    loadDrivers(driverName);
	    createPool();
	}
}

//װ�غ�ע������JDBC�����
private void loadDrivers(String dri) {

    String driverClassName = dri;
    try {
      driver = (Driver) Class.forName(driverClassName).newInstance();
      DriverManager.registerDriver(driver);
//      System.out.println("�ɹ�ע��JDBC�����" + driverClassName);
    }
    catch (Exception e) {
//      System.out.println("�޷�ע��JDBC�����:" + driverClassName + ",����:" + e);
    	e.printStackTrace();
    }
}

//�������ӳ�
private void createPool() {
    pool = new DBConnectionPool(password, url, user, normalConnect, maxConnect);
    if (pool != null) {
//      System.out.println("�������ӳسɹ�");
    }
    else {
//      System.out.println("�������ӳ�ʧ��");
    }
}

//���һ�����õ�����,���û���򴴽�һ������,��С�������������
public Connection getConnection() {
    if (pool != null) {
      return pool.getConnection();
    }
    return null;
}

//���һ������,��ʱ������
public Connection getConnection(long time) {
    if (pool != null) {
      return pool.getConnection(time);
    }
    return null;
}

//�����Ӷ��󷵻ظ����ӳ�
public void freeConnection(Connection con) {
    if (pool != null) {
      pool.freeConnection(con);
    }
}

//���ص�ǰ����������
public int getnum()
{
    return pool.getnum();
}


//���ص�ǰ������
public int getnumActive()
{
    return pool.getnumActive();
}


//�ر���������,������ע��
public synchronized void release() {

    ///�ر�����
    pool.release();

    ///������
    try {
      DriverManager.deregisterDriver(driver);
//      System.out.println("����JDBC����� " + driver.getClass().getName());
    }
    catch (SQLException e) {
//      System.out.println("�޷�����JDBC������ע��:" + driver.getClass().getName());
    }

}

}
